import { Controller } from "../../mvc/core/Controller";
import { RechargeActiveBtnIcon } from "../view/loginNotice/OpenRecharge/RechargeActiveBtnIcon";
import { IView } from "../../mvc/interfaces/IView";
import { FirstRechargeModule } from "../../../modules/FirstRechargeModule";
import { IRechargeGiftBagModule } from "../../../modules/interfaces/IRechargeGiftBagModule";
import { RechargeGiftBagModule } from "../../../modules/RechargeGiftBagModule";
import { FirstRechargeNewModule } from "../../../modules/FirstRechargeNewModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { ParamsConst } from "../../common/global/ParamsConst";
import { ProxyType } from "../../common/global/ProxyType";
import { RechargeData } from "../view/loginNotice/OpenRecharge/RechargeData";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { IFirstRechargeModule } from "../../../modules/interfaces/IFirstRechargeModule";
//class FirstRechargeController
    
    export  class FirstRechargeController extends Controller
    {
       

        public get rechargeActiveBtn(): RechargeActiveBtnIcon
        {
            if (this._rechargeActiveBtn == null) 
            {
                this._rechargeActiveBtn = RechargeActiveBtnIcon.instance;
            }
            return this._rechargeActiveBtn;
        }

        protected /* override */ initView(): IView
        {
            if (this._firstRechargeModule == null) 
            {
                this._firstRechargeModule = new FirstRechargeModule();
            }
            return this._firstRechargeModule;
        }

        public get rechargeGiftBagModule(): IRechargeGiftBagModule
        {
            if (this._rechargeGiftBagModule == null) 
            {
                this._rechargeGiftBagModule = new RechargeGiftBagModule();
            }
            return this._rechargeGiftBagModule;
        }

        public get firstRechargeNew(): FirstRechargeNewModule
        {
            if (this._firstRechargeNew == null) 
            {
                this._firstRechargeNew = new FirstRechargeNewModule();
            }
            return this._firstRechargeNew;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.FirstRechargeGiftOpen, this.onFirstRechargeGiftOpenHandler);
            Dispatcher.addEventListener(EventName.HavePay, this.onHavePayHandler);
            Dispatcher.addEventListener(EventName.GetOpenRechargeGiftBagSuccess, this.onGetOpenRechargeGiftBagSuccessHandler);
            Dispatcher.addEventListener(EventName.RechargeActiveWindowOpen, this.onRechargeActiveWindowOpenHandler);
            Dispatcher.addEventListener(EventName.NoviceCardUseSuccess, this.onNoviceCardUseSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.RechargeGiftBagActiveCommand, this.onRechargeGiftBagActiveHandler);
            return;
        }

        /* internal  */onFirstRechargeGiftOpenHandler(arg1: DataEvent): void
        {
            if (ParamsConst.instance.proxyType == ProxyType.TW) 
            {
                view.show();
                return;
            }
            this.firstRechargeNew.show();
            return;
        }

        /* internal  */onHavePayHandler(arg1: DataEvent): void
        {
            if (this._firstRechargeModule && !this._firstRechargeModule.isHide) 
            {
                this._firstRechargeModule.hide();
            }
            if (this._firstRechargeNew && !this._firstRechargeNew.isHide) 
            {
                this._firstRechargeNew.hide();
            }
            if (RechargeData.isRechargeActive) 
            {
                if (this.rechargeActiveBtn.isHide) 
                {
                    this.rechargeActiveBtn.show();
                    this.rechargeGiftBagModule.show();
                }
                if (this._rechargeGiftBagModule && !this._rechargeGiftBagModule.isHide) 
                {
                    this.rechargeGiftBagModule.updateRechargeMoney();
                }
            }
            return;
        }

        /* internal  */onGetOpenRechargeGiftBagSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            RechargeData.hadGetCodes = loc1;
            if (RechargeData.isRechargeActive && this._rechargeGiftBagModule && !this._rechargeGiftBagModule.isHide) 
            {
                this._rechargeGiftBagModule.updateGiftBagInfo();
            }
            return;
        }

        /* internal  */onNoviceCardUseSuccessHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onRechargeActiveWindowOpenHandler(arg1: DataEvent): void
        {
            if (RechargeData.hadGetCodes == null) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GetOpenRechargeGiftBagHadGetCodes));
            }
            this.rechargeGiftBagModule.show();
            return;
        }

        /* internal  */onRechargeGiftBagActiveHandler(arg1: SActiveToClient): void
        {
            if (RechargeData.hadGetCodes == null) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GetOpenRechargeGiftBagHadGetCodes));
            }
            RechargeData.activeToClient = arg1;
            if (cache.role.recharge > 0) 
            {
                if (this.rechargeActiveBtn.isHide) 
                {
                    this.rechargeActiveBtn.show();
                    this.rechargeGiftBagModule.show();
                }
            }
            return;
        }

        private /* var */_firstRechargeModule: IFirstRechargeModule;

        private /* var */_rechargeActiveBtn: RechargeActiveBtnIcon;

        private /* var */_firstRechargeNew: FirstRechargeNewModule;

        private /* var */_rechargeGiftBagModule: IRechargeGiftBagModule;
    }
